package comic;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;

import android.graphics.drawable.Drawable;

public class OrderOfTheStick extends Comic {

	private static final long serialVersionUID = 894461776057609300L;

	@Override
	public String get_display_name() {
		return "Order Of The Stick";
	}

	@Override
	public int get_first_comic_n() {
		return 1;
	}

	@Override
	public int getLatest() throws MalformedURLException, IOException {
		String content = get_website_content("http://www.giantitp.com/comics/oots.html");
		String result = content.replaceAll(
				".*<BR>\\s*<P class=\"ComicList\">(\\d+).*", "$1");

		return Integer.parseInt(result);
	}

	@Override
	public Strip getStrip(int id) throws MalformedURLException, IOException {
		int latest = getLatest();
		Strip s = new Strip();

		if (id < first_comic)
			id = first_comic;
		else if (id > latest)
			id = latest;
		s.id = id;

		String url = "http://www.giantitp.com/comics/oots.html";
		String content = get_website_content(url);
		String regex = String.format(
				".*<A href=\"/comics/oots%04d.html\">(.*?)</A>.*", id);
		s.title = content.replaceAll(regex, "$1");

		url = String.format("http://www.giantitp.com/comics/oots%04d.html", id);
		content = get_website_content(url);
		regex = ".*/comics/images/(.*?)\".*";
		String result = content.replaceAll(regex, "$1");
		URL image_url = new URL("http://www.giantitp.com/comics/images/"
				+ result); // result is a relative link

		s.image = Drawable.createFromStream(image_url.openStream(), "");

		s.text = "";

		return s;
	}

}
